<?php

namespace App\Http\Controllers;

use App\Events\ClickLogEvent;
use App\Models\ClickLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LogController extends Controller
{
    /**
     * 购买点击日志
     */
    public function payNowLogs(Request $request)
    {
        $user_id = Auth::id();
        $goods_id = $request->input('goods_id');
        $ip = $request->header('X-Real-IP') ?? $request->ip();
        $userAgent = $request->header('User-Agent');
        $isMobileDevice = preg_match('/(android|iphone|ipod|blackberry|windows phone)/i', $userAgent);

        $clickLog = ClickLog::create([
            'user_id' => $user_id,
            'ip' => $ip,
            'event_type' => 'select_goods',
            'event_data' => json_encode([
                'goods_id' => $goods_id
            ]),
            'goods_id' => $goods_id,
            'is_mobile' => (bool)$isMobileDevice,
            'user_agent' => $userAgent,
            'create_day' => date('Y-m-d'),
        ]);
        event(new ClickLogEvent($clickLog));
    }
}
